var express = require('express')

//创建子路由对象（用来管理一部分后端路由）
var goodRouter = express.Router()

//设置接口
//分页查询
goodRouter.get('/list', (req, res) => {
    var { page, count } = req.query
    console.log(page, count);

    //分页
    var newarr = arr.slice((page - 1) * count, page * count)

    // console.log('收到来自客户端的请求');
    res.send({ code: 200, msg: '请求成功！', list: newarr })
})

//模糊搜索
goodRouter.get('/search', (req, res) => {
    var { searchkey } = req.query
    //根据参数，从用户的name属性中过滤
    var newarr = arr.filter((item) => {
        // return item.name.indexOf(searchkey) != -1
        //includes() 返回 true/false 
        return item.name.includes(searchkey)
        //return new RegExp(searchkey).test(item.name)
    })
    res.send({ code: 200, msg: '查询成功', list: newarr })
})


//导出子路由对象
module.exports = goodRouter
